package model

import "time"

type CustomerTelephone struct {
	Id int64 `xorm:"pk autoincr"`
	CustomerId int64
	CustomerName string
	Phone string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type AppRegistrationInformation struct {
	Id int64 `xorm:"pk autoincr"`
	UserId int64
	Phone string
	Level int
	CreatedAt time.Time
	UpdatedAt time.Time
}

type CustomerMainPhone struct {
	Id int64 `xorm:"pk autoincr"`
	CustomerId int64
	CustomerName string
	Phone string
	MungBeanValue int64
	CreatedAt time.Time
	UpdatedAt time.Time
}

type TelephoneInformation struct {
	Phone string
	Level int
	AppRegister bool
	MainPhone bool
	RegisterTimeUnix int64
	CreationTimeUnix int64
	RegisterTime string
	CreationTime string
}

func BubbleSortLevel(arr []*TelephoneInformation) []*TelephoneInformation{
	for i := 0; i < len(arr)-1; i++ {
		for j := 0; j < len(arr)-i-1; j++ {
			if arr[j].Level < arr[j+1].Level {
				//temp := arr[j]
				//arr[j] = arr[j+1]
				//arr[j+1] = temp

				arr[j], arr[j+1] = arr[j+1], arr[j]
			}
		}
	}
	return arr
}

func BubbleSortRegisterTimeUnix(arr []*TelephoneInformation) []*TelephoneInformation{
	for i := 0; i < len(arr)-1; i++ {
		for j := 0; j < len(arr)-i-1; j++ {
			if arr[j].RegisterTimeUnix > arr[j+1].RegisterTimeUnix {
				//temp := arr[j]
				//arr[j] = arr[j+1]
				//arr[j+1] = temp

				arr[j], arr[j+1] = arr[j+1], arr[j]
			}
		}
	}
	return arr
}

func (telephoneInformation *TelephoneInformation)QueryPhoneInfo(customerId int64)([]*TelephoneInformation, error){

	var ctList []*CustomerTelephone
	x.Where("customer_id = ?", customerId).Asc("created_at").Find(&ctList)

	var tiList []*TelephoneInformation
	var registerTiList []*TelephoneInformation

	var main bool = false
	for _, ct := range ctList{
		var ti = TelephoneInformation{
			Phone:        ct.Phone,
			CreationTimeUnix: ct.CreatedAt.Unix(),
			CreationTime: ct.CreatedAt.Format("2006-01-02 15:04:05"),
		}

		var ari AppRegistrationInformation
		x.Where("phone = ?", ti.Phone).Get(&ari)
		if ari.Id != 0{
			ti.AppRegister = true
			ti.Level = ari.Level
			ti.RegisterTimeUnix = ari.CreatedAt.Unix()
			ti.RegisterTime = ari.CreatedAt.Format("2006-01-02 15:04:05")
			registerTiList = append(registerTiList, &ti)
		}

		var cmp CustomerMainPhone
		x.Where("customer_id = ?", customerId).And("phone = ?", ti.Phone).Get(&cmp)
		if cmp.Id != 0{
			ti.MainPhone = true
			main = true
		}

		tiList = append(tiList, &ti)
	}

	if main == true{
		return tiList, nil

	}else {
		if registerTiList == nil{
			tiList[0].MainPhone = true
		}else if len(registerTiList) == 1{
			registerTiList[0].MainPhone = true
		}else {
			var levelTiList []*TelephoneInformation
			bubbleSortregisterTiList := BubbleSortLevel(registerTiList)
			for _, bubbleSortregisterTi := range bubbleSortregisterTiList{
				if bubbleSortregisterTiList[0].Level == bubbleSortregisterTi.Level{
					levelTiList = append(levelTiList, bubbleSortregisterTi)
				}
			}

			if len(levelTiList) == 1{
				bubbleSortregisterTiList[0].MainPhone = true
			}else{
				registerTimeUnixTiList := BubbleSortRegisterTimeUnix(levelTiList)
				registerTimeUnixTiList[0].MainPhone = true
			}
		}
	}

	return tiList, nil
}




































